<template>
  <el-card>
    <template #header>
      <div class="card-header flex justify-between">
        <div>
          <span>字典详情</span>
          <span v-if="activeDict && activeDict.id" class="text-[12px]">
            当前选中的字典为({{ activeDict.name }})
          </span>
        </div>
        <el-button class="button" type="primary" :icon="Plus" @click="addMenu"
          >新增</el-button
        >
      </div>
    </template>
    <div class="main w-full">
      <div v-if="activeDict && activeDict.id">
        <el-table v-loading="loading" :data="tableData.data">
          <el-table-column label="所属字典" prop="parent" />
          <el-table-column label="字典标签" prop="label" />
          <el-table-column label="字典值" prop="value" />
          <el-table-column label="排序" prop="dictSort" />
          <el-table-column fixed="right" label="操作" width="120">
            <template #default="scope">
              <el-button
                type="primary"
                size="small"
                :icon="Edit"
                @click="editRow(scope.row)"
              />
              <el-popconfirm
                width="200px"
                title="确定删除本条数据吗？"
                @confirm="delConfirm(scope.row)"
              >
                <template #reference>
                  <el-button type="danger" size="small" :icon="Delete" />
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <div class="page w-full mt-[10px]">
          <el-pagination
            v-model:current-page="pageObj.data.currentPage"
            v-model:page-size="pageObj.data.pageSize"
            :page-sizes="[10, 20, 30, 40, 50, 100]"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageObj.data.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
      <div v-else>
        <el-divider content-position="left">点击字典查看详情</el-divider>
      </div>
    </div>
    <el-dialog
      v-model="addDialog"
      :close-on-click-modal="false"
      :before-close="beforeClose"
      :title="title"
      width="580px"
    >
      <el-form
        ref="formRef"
        :model="form.data"
        :rules="form.rules"
        label-width="80px"
      >
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="字典标签" prop="label">
              <el-input v-model="form.data.label" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="字典值" prop="label">
              <el-input v-model="form.data.value" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="排序" prop="dictSort">
              <el-input-number
                v-model.number="form.data.dictSort"
                :min="0"
                :max="999"
                controls-position="right"
                style="width: 100%"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <template #footer>
        <span class="dialog-footer">
          <el-button @click="cancel">取消</el-button>
          <el-button type="primary" @click="submit"> 确认 </el-button>
        </span>
      </template>
    </el-dialog>
  </el-card>
</template>

<script setup lang="ts">
import { Delete, Edit, Plus } from "@element-plus/icons-vue";
import { nextTick, reactive, ref, watch } from "vue";
import {
  getDictData,
  addDictData,
  editDictData,
  delDictData,
  addJobApi,
  editJobApi,
  delJobApi
} from "@/api/system";
import { ElForm, ElNotification } from "element-plus";
import { cloneDeep } from "@pureadmin/utils";
const props = defineProps({
  activeDict: {
    type: Object,
    default() {
      return {
        id: "",
        name: "",
        dictDetails: []
      };
    }
  }
});
const pageObj = reactive({
  data: {
    currentPage: 1,
    pageSize: 10,
    total: 0
  }
});
const loading = ref(false);
const getDictList = async () => {
  loading.value = true;
  const { content, totalElements } = await getDictData({
    dictName: props.activeDict.name,
    page: pageObj.data.currentPage - 1,
    size: pageObj.data.pageSize
  });
  pageObj.data.total = totalElements;
  tableData.data = content;
  tableData.data.forEach(item => {
    item.parent = props.activeDict.name;
  });
  loading.value = false;
};
const handleSizeChange = () => {
  pageObj.data.currentPage = 1;
  getDictList();
};
const handleCurrentChange = async () => {
  getDictList();
};
watch(
  () => props.activeDict,
  n => {
    pageObj.data.currentPage = 1;
    getDictList();
  },
  {
    deep: true
  }
);
const tableData = reactive({
  data: []
});
const form = reactive({
  data: {
    id: null,
    label: null,
    value: null,
    dictSort: 999
  },
  rules: {
    label: [{ required: true, message: "请输入字典标签", trigger: "blur" }],
    value: [{ required: true, message: "请输入字典值", trigger: "blur" }],
    dictSort: [
      { required: true, message: "请输入序号", trigger: "blur", type: "number" }
    ]
  }
});
const formRef = ref<InstanceType<typeof ElForm>>();
const addDialog = ref(false);
const title = ref("新增字典详情");
const status = ref("add");
const addMenu = () => {
  addDialog.value = true;
  title.value = "新增字典详情";
  status.value = "add";
};
const cancel = () => {
  form.data = {
    id: null,
    label: null,
    value: null,
    dictSort: 999
  };
  formRef.value.resetFields();
  nextTick(() => {
    addDialog.value = false;
  });
};
const editRow = async row => {
  addDialog.value = true;
  title.value = "编辑字典详情";
  status.value = "edit";
  let data = cloneDeep(row);
  form.data = data;
};
const resetForm = () => {
  form.data = {
    id: null,
    label: null,
    value: null,
    dictSort: 999
  };
  formRef.value.resetFields();
};
const submit = () => {
  formRef.value.validate(async valid => {
    if (valid) {
      let data = cloneDeep(form.data);
      data.dict = {
        id: props.activeDict.id
      };
      if (status.value === "add") {
        await addDictData(data);
        ElNotification({
          title: "系统提示",
          message: "字典详情创建成功",
          type: "success"
        });
      } else {
        await editDictData(data);
        ElNotification({
          title: "系统提示",
          message: "字典详情编辑成功",
          type: "success"
        });
      }
      resetForm();
      nextTick(() => {
        addDialog.value = false;
        pageObj.data.currentPage = 1;
        getDictList();
      });
    }
  });
};
const delConfirm = async row => {
  await delDictData(row.id);
  ElNotification({
    title: "系统提示",
    message: "删除成功",
    type: "success"
  });
  pageObj.data.currentPage = 1;
  getDictList();
};
const beforeClose = d => {
  form.data = {
    id: null,
    label: null,
    value: null,
    dictSort: 999
  };
  nextTick(() => {
    d();
  });
};
</script>
